package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;
import java.util.List;

public class Arrow extends AbstractMapleStoryObject{
    public  static Image[] imgs = {
            ImageUtil.getImage("hero_right_arrow"),
            ImageUtil.getImage("hero_left_arrow"),
    };


    public Arrow () {
        this.speed = 20;
        this.live = true;
        this.width = imgs[0].getWidth(null);
        this.height = imgs[0].getHeight(null);
    }

    public Arrow (MapleStoryClient msc,int x, int y, Direction dir) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;


    }

    @Override
    public void draw(Graphics g) {
        if (!live){
            msc.arrows.remove(this);
            return;
        }
        switch (dir){
            case RIGHT:
                g.drawImage(imgs[0],x,y,null);
                break;
            case LEFT:
                g.drawImage(imgs[1],x,y,null);
                break;
            default:
                break;
        }
        move();
    }

    @Override
    public void move() {
        switch (dir){
            case RIGHT:
                this.x += speed;
                break;
            case LEFT:
                this.x -= speed;
                break;
            default:
                break;
        }
        outOfBound();
    }

    /**
     * 出界判断
     */
    private void outOfBound(){
        if (this.x < -500 || this.x > Constant.GAME_WIDTH + 500) {
            this.live = false;
        }
    }

    /**
     *弓箭hit怪物的方法
     * @param mob 被射击中的怪
     * @return 是否击中
     */
    private boolean hit(Mob mob){
        if (this.getRectangle().intersects(mob.getRectangle()) && mob.action != Action.DIE) {
            this.live = false;
            mob.action = Action.DIE;
//            mob.live = false;
            return true;
        }
        return false;
    }

    /**
     * 一支弓箭与所有的怪物进行碰撞检测方法
     * @param mobs 怪物容器
     * @return 是否击中
     */
    public boolean hit(List<Mob> mobs){
        for (int i = 0; i <mobs.size() ; i++) {
            Mob mob = mobs.get(i);
            if (hit(mob)) {
                return true;
            }
        }
        return false;
    }
}

